package com.xinqi.modules.course.curriculum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.course.curriculum.dto.req.CourseCurriculumCreateDTO;
import com.xinqi.modules.course.curriculum.dto.req.CourseCurriculumUpdateDTO;
import com.xinqi.modules.course.curriculum.dto.rsp.CourseCurriculumResultDTO;
import com.xinqi.modules.course.curriculum.convert.CourseCurriculumConvert;
import com.xinqi.modules.course.curriculum.domain.CourseCurriculumEntity;
import com.xinqi.modules.course.curriculum.manager.CourseCurriculumManager;
import com.xinqi.modules.course.curriculum.service.CourseCurriculumService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 课程表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/09/04
 */
@Service("courseCurriculumService")
@RequiredArgsConstructor
public class CourseCurriculumServiceImpl implements CourseCurriculumService {
    private final CourseCurriculumConvert courseCurriculumConvert;
    private final CourseCurriculumManager courseCurriculumManager;

    /**
     * 根据id查询 课程表数据
     *
     * @param id
     * @return
     */
    @Override
    public CourseCurriculumResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.curriculum.id.not.blank"));
        return Optional.ofNullable(courseCurriculumManager.getById(id)).map(courseCurriculumConvert::convert).orElse(null);
    }

    /**
     * 根据用户ID获取课程表数据
     *
     * @param userId
     * @return
     */
    @Override
    public List<CourseCurriculumResultDTO> listByUser(Long userId) {
        LambdaQueryWrapper<CourseCurriculumEntity> wrapper = Queries.lambda();
        wrapper.eq(CourseCurriculumEntity::getUserId, userId)
            .orderByDesc(CourseCurriculumEntity::getCreateTime);
        return courseCurriculumManager.list(wrapper).stream()
            .map(courseCurriculumConvert::convert).collect(Collectors.toList());
    }

    /**
     * 新增 课程表数据
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(CourseCurriculumCreateDTO dto) {
        Long userId = LoginHelper.getUserId();
        CourseCurriculumEntity entity = courseCurriculumConvert.create(dto);
        entity.setUserId(userId);
        courseCurriculumManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 课程表数据
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(CourseCurriculumUpdateDTO dto) {
        CourseCurriculumEntity entity = Optional.ofNullable(courseCurriculumManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("data.does.not.exist")));

        Long userId = LoginHelper.getUserId();

        CommonErrors.BAD_REQUEST.check(Objects.equals(entity.getUserId(), userId), MessageUtils.message("course.curriculum.is.not.user"));
        CourseCurriculumEntity update = courseCurriculumConvert.update(dto);
        update.setUserId(userId);
        return courseCurriculumManager.updateById(update);
    }

    /**
     * 根据id删除 课程表数据
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CourseCurriculumEntity entity = Optional.ofNullable(courseCurriculumManager.getById(id))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("data.does.not.exist")));

        Long userId = LoginHelper.getUserId();

        CommonErrors.BAD_REQUEST.check(Objects.equals(entity.getUserId(), userId), MessageUtils.message("course.curriculum.is.not.user"));
        return courseCurriculumManager.removeById(id);

    }

}
